
#ifndef  IBREAKPOINTSTATEEVENT_H
#define  IBREAKPOINTSTATEEVENT_H
#include <ostream>

class BreakPointService;

//State-Event Table
//                start,    connect, LoginResp, timeout, BreakPointResp, NormalResp, disconnect,   stop,   sendMessage
//_end:          ac/->_init  -1         -1          -1          -1          -1          -1            -1         -1
//_inited:         -1        ac/->_con  -1          -1          -1          -1          -1           ->_end/ac   -1
//_connected:      -1        -1       ->_logined    -1          0           0         ->discon/ac    ->_end/ac   0
//_logined:        -1        -1         0           act-bp      act         act       ->discon/ac    ->_end/ac   0
//_disconnected:   -1        ac/->_con  -1          -1          -1          -1          -1           ->_end/ac   -1

class IBreakPointStateEvent {
public:
    IBreakPointStateEvent(){}
    virtual ~IBreakPointStateEvent(){}

    virtual int start(BreakPointService * ctx) {return -1;}
    virtual int stop(BreakPointService * ctx)  {return -1;}

    virtual int connected(BreakPointService * ctx) {return -1;}
    virtual int disconnected(BreakPointService * ctx) {return -1;}

    virtual int timeout(BreakPointService * ctx) {return -1;}

    virtual int receivedLoginResp(BreakPointService * ctx, const char * msg) {return -1;}
    virtual int receivedNormalResp(BreakPointService * ctx, const char * msg) {return -1;}
    virtual int receivedBreakPointResp(BreakPointService * ctx, const char * msg) {return -1;}

    virtual int sendMessage(BreakPointService * ctx, const char * msg) {return -1;}

    virtual std::ostream &  log(std::ostream & out);
};


//                start,   connect, LoginResp, timeout, BreakPointResp, NormalResp, disconnect,   stop,   sendMessage
//_end:  adFromFile/->_init  -1         -1          -1          -1          -1          -1            -1         -1
class BreakPointEndState : public IBreakPointStateEvent {
public:
    static BreakPointEndState * newInstance();
    virtual ~BreakPointEndState(){}

    int start(BreakPointService * ctx) override;

    std::ostream &  log(std::ostream & out) override;
private:
    static BreakPointEndState * inst;
};

//                start,   connect, LoginResp, timeout, BreakPointResp, NormalResp, disconnect,   stop,   sendMessage
//_inited:         -1  login()/->_con  -1          -1          -1          -1          -1    ->_end/saveToFile   -1
class BreakPointInitedState : public IBreakPointStateEvent {
public:
    static BreakPointInitedState * newInstance();
    virtual ~BreakPointInitedState(){}

    int stop(BreakPointService * ctx) override;
    int connected(BreakPointService * ctx) override;

    std::ostream &  log(std::ostream & out) override;
private:
    static BreakPointInitedState * inst;
};

//                start,   connect, LoginResp,              timeout, BreakPointResp, NormalResp, disconnect,   stop,            sendMessage
//_connected:      -1        -1  handleLoginRsp/->_logined    -1          0           0           ->discon  ->_end/saveToFile   sendMessage
class BreakPointConnectedState : public IBreakPointStateEvent {
public:
    static BreakPointConnectedState * newInstance();
    virtual ~BreakPointConnectedState(){}

    int receivedLoginResp(BreakPointService * ctx, const char * msg) override;
    int receivedBreakPointResp(BreakPointService * ctx, const char * msg) override {return 0;}
    int receivedNormalResp(BreakPointService * ctx, const char * msg) override { return 0;}

    int disconnected(BreakPointService * ctx) override;
    int stop(BreakPointService * ctx) override;

    int sendMessage(BreakPointService * ctx, const char * msg) override;


    std::ostream &  log(std::ostream & out) override;
private:
    static BreakPointConnectedState * inst;
};

//                start,   connect, LoginResp, timeout, BreakPointResp, NormalResp,      disconnect,    stop,           sendMessage
//_logined:        -1        -1         0      bpQuery  handleBpResp    handleNmRespact    ->discon  ->_end/saveToFile  sendMessage
class BreakPointLoginedState : public IBreakPointStateEvent {
public:
    static BreakPointLoginedState * newInstance();
    virtual ~BreakPointLoginedState(){}


    int receivedLoginResp(BreakPointService * ctx, const char * msg) override {return 0;}

    int timeout(BreakPointService * ctx) override;

    int receivedBreakPointResp(BreakPointService * ctx, const char * msg) override;
    int receivedNormalResp(BreakPointService * ctx, const char * msg) override;

    int disconnected(BreakPointService * ctx) override;
    int stop(BreakPointService * ctx) override;

    int sendMessage(BreakPointService * ctx, const char * msg) override;


    std::ostream &  log(std::ostream & out) override;
private:
    static BreakPointLoginedState * inst;
};

//                start,   connect,     LoginResp, timeout, BreakPointResp, NormalResp, disconnect,   stop,     sendMessage
//_disconnected:   -1       login()/->_con  -1          -1          -1          -1          -1   ->_end/saveToFile   -1
class BreakPointDisconnectedState : public IBreakPointStateEvent {
public:
    static BreakPointDisconnectedState * newInstance();
    virtual ~BreakPointDisconnectedState(){}


    int connected(BreakPointService * ctx) override;
    int stop(BreakPointService * ctx) override;

    std::ostream &  log(std::ostream & out) override;
private:
    static BreakPointDisconnectedState * inst;
};


#endif   /* IBREAKPOINTSTATEEVENT_H */
